// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// G [author] rhyme_lph
// u [email]  rhymelph@gmail.com
// H [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/leave_type_entity.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/leave/select_adjustment_leave_type_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "selectAdjustmentLeaveTypePage")
class SelectAdjustmentLeaveTypePage extends StatefulWidget {
  @override
  _SelectAdjustmentLeaveTypePageState createState() =>
      _SelectAdjustmentLeaveTypePageState();
}

class _SelectAdjustmentLeaveTypePageState
    extends State<SelectAdjustmentLeaveTypePage>
    with
        PagePresenter<SelectAdjustmentLeaveTypePage,
            SelectAdjustmentLeaveTypeProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.leave_type,
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: rootListBuilder<List<LeaveTypeEntity>>(
          selector: (_, __) => List.from(Config.settingData.leaveType),
          builder: (BuildContext context, List<LeaveTypeEntity> value,
                  Widget child) =>
              ListView.builder(
            padding: EdgeInsets.only(
              top: getDp(20),
            ),
            itemBuilder: _buildItems,
            itemCount: value.length,
          ),
        ),
      );

  Widget _buildItems(BuildContext context, int index) {
    return rootListBuilder<LeaveTypeEntity>(
      selector: (_, __) => Config.settingData.leaveType[index],
      builder: (BuildContext context, LeaveTypeEntity e, Widget child) =>
          PressedOpacityButton(
        onPressed: () => provider.onSelectLeaveType(e),
        padding: EdgeInsets.symmetric(
          vertical: getDp(10),
          horizontal: getDp(10),
        ),
        child: NormalRadio(
          title: e.name.replaceAll('(Days)', ''),
          isSelect: false,
          isFix: true,
        ),
      ),
    );
  }
}
